<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page import="java.sql.*" %>	

<%String callback = request.getParameter("callback");%>
<%=callback %> (<%

	String regid = request.getParameter("regid");
	String id = request.getParameter("id");
        String device = request.getParameter("device");

	Connection conn = null;
	PreparedStatement pstmt = null;
	PreparedStatement regpstmt = null;
	try {

		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://localhost/itscreater", "itscreater", "campus123");
		  
		String regidselect = "select * from gcm where ID = ? AND Device = ?;";
		regpstmt = conn.prepareStatement(regidselect);
		regpstmt.setString(1, id);
	        regpstmt.setString(2, device);
	        ResultSet rs = regpstmt.executeQuery();
		
		if (rs.next()) {
			String update = "update gcm set regid:= ?" +
						"where ID = ? AND Device = ?;";
			pstmt = conn.prepareStatement(update);
			pstmt.setString(1, regid);
			pstmt.setString(2, id);
			pstmt.setString(3, device);
			pstmt.executeUpdate();
		} else {
			String insert = "insert into gcm" + "(ID, regid, Device) " +
					"values (?, ?, ?);";
			pstmt = conn.prepareStatement(insert);
			pstmt.setString(1, id);
			pstmt.setString(2, regid);
			pstmt.setString(3, device);
			pstmt.executeUpdate();
		}
	}
				
	
	catch (Exception e) {


	}
	finally{
		try{
			pstmt.close();
		}
		catch (Exception ignored){
		}
		try{
			regpstmt.close();
		}
		catch (Exception ignored){
		}
		try{
			conn.close();
		}
		catch (Exception ignored){
		}
	}
%>);